<!--
 Copyright 2025 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<div class="eval-container">
  @if (selectedEvalSet == '') {
    <div class="eval-set-actions">
      <div>{{ i18n.allEvalSetsHeader }}</div>
      <mat-icon style="cursor: pointer;" (click)="openNewEvalSetDialog()" [matTooltip]="i18n.createNewEvalSetTooltip">add</mat-icon>
    </div>
  }
  @if (evalsets.length == 0) {
    <div>
      <div class="empty-eval-info">
        <div class="info-title">
          {{ i18n.createNewEvalSetTitle }}
        </div>
        <div class="info-detail">
          {{ i18n.evalSetDescription }}
        </div>
        <div class="info-create" (click)="openNewEvalSetDialog()">
          {{ i18n.createEvalSetButton }}
        </div>
      </div>
    </div>
  }
  @if (evalsets.length > 0 && this.selectedEvalSet == '') {
    <div>
      @for (set of evalsets; track set) {
        <div class="eval-set-row" (click)="selectEvalSet(set)">
          <div style="display: flex;">
            <span class="material-symbols-outlined" style="margin-right:10px; padding-top: 16px;">folder</span>
            <div style="font-family: Roboto;font-size: 14px; padding: 16px; padding-top: 20px;">{{set}}</div>
          </div>
          <div>
            <mat-icon style="padding-top: 20px; color:#9AA0A6;">chevron_right</mat-icon>
          </div>
        </div>
      }
    </div>
  }
  @if (selectedEvalSet != '') {
    <div>
      <div style="display: flex;">
        <mat-icon style="color:white;cursor: pointer;" (click)="clearSelectedEvalSet()">chevron_left</mat-icon>
        <div style="color:#9AA0A6; padding-top:2px;cursor: pointer;" (click)="clearSelectedEvalSet()">
          {{selectedEvalSet}}
        </div>
      </div>
      @if (evalCases.length > 0 && !evalRunning()) {
        <div>
          @if (!showEvalHistory()) {
            <div>
              <div class="evaluation-tab-header">
                <button class="run-eval-btn" (click)="openEvalConfigDialog()">{{ i18n.runEvaluationButton }}</button>
                <mat-icon class="evaluation-history-icon" (click)="toggleEvalHistoryButton()" [matTooltip]="i18n.viewEvalRunHistoryTooltip">history</mat-icon>
              </div>
              <div class="mat-table-container" style="margin-top:16px;">
                <table mat-table [dataSource]="dataSource">
                  <ng-container matColumnDef="select">
                    <th mat-header-cell *matHeaderCellDef>
                      <mat-checkbox (change)="$event ? toggleAllRows() : null"
                        [checked]="selection.hasValue() && isAllSelected()"
                        [indeterminate]="selection.hasValue() && !isAllSelected()">
                      </mat-checkbox>
                    </th>
                    <td mat-cell *matCellDef="let row">
                      <mat-checkbox (click)="$event.stopPropagation()"
                        (change)="$event ? selection.toggle(row) : null"
                        [checked]="selection.isSelected(row)">
                      </mat-checkbox>
                    </td>
                  </ng-container>
                  <!-- Column Definitions -->
                  <ng-container matColumnDef="evalId">
                    <th mat-header-cell *matHeaderCellDef> {{ i18n.caseIdHeader }} </th>
                    <td mat-cell class="eval-case-id" [class.selected-eval-case]="element === selectedEvalCase()?.evalId" *matCellDef="let element" (click)="getEvalCase(element)"> {{element}} </td>
                  </ng-container>
                  <ng-container matColumnDef="finalEvalStatus">
                    <th mat-header-cell *matHeaderCellDef> {{ i18n.resultHeader }} </th>
                    <td mat-cell *matCellDef="let element">
                      @if (getEvalResultForCase(element)) {
                        <button
                          [ngClass]="getEvalResultForCase(element) == 1 ? 'result-btn pass': 'result-btn fail'" (click)="getSession(element)" [matTooltip]="i18n.viewEvalRunResultTooltip">
                          <span class="material-symbols-outlined">
                            {{ getEvalResultForCase(element) == 1 ? "check": "close" }}
                          </span>
                          <div style="padding-top: 4px;">{{getEvalResultForCase(element) == 1 ? i18n.passStatus: i18n.failStatus}} </div>
                        </button>
                      }
                    </td>
                  </ng-container>
                  <!-- Table Rows -->
                  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
                  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
                </table>
              </div>
            </div>
          }
          @if (showEvalHistory()) {
            <div>
              @for (evalResult of getEvalHistoryOfCurrentSetSorted(); track evalResult) {
                <div>
                  <div class="status-card">
                    <div class="status-card__overview">
                      <div class="status-card__info">
                        <div class="status-card__timestamp">{{ formatTimestamp(evalResult.timestamp) }}</div>
                        <div class="status-card__summary">
                          <span class="status-card__passed">{{ getPassCountForCurrentResult(evalResult.evaluationResults.evaluationResults) }} {{ i18n.passedSuffix }}</span>
                          @if (getFailCountForCurrentResult(evalResult.evaluationResults.evaluationResults) > 0) {
                            <div>
                              <span class="status-card__separator">|</span>
                              <span class="status-card__failed">{{ getFailCountForCurrentResult(evalResult.evaluationResults.evaluationResults) }} {{ i18n.failedSuffix }}</span>
                            </div>
                          }
                        </div>
                        @if (getEvalMetrics(evalResult)) {
                          <div class="status-card__metrics">
                            @for (evalMetric of getEvalMetrics(evalResult); track evalMetric) {
                              <span class="status-card__metric"> {{ evalMetric.metricName }}:
                                {{ evalMetric.threshold }}
                              </span>
                            }
                          </div>
                        }

                      </div>
                      <div class="status-card__action">
                        <mat-icon (click)="toggleHistoryStatusCard(evalResult.timestamp)">{{ getEvaluationStatusCardActionButtonIcon(evalResult.timestamp) }}</mat-icon>
                      </div>
                    </div>
                    @if (isEvaluationStatusCardToggled(evalResult.timestamp)) {
                      <div class="status-card__history-cases">
                        @for (evalResult of generateHistoryEvaluationDatasource(evalResult.timestamp); track evalResult) {
                          <div>
                            <div class="status-card__history-case">
                              <span> {{ evalResult.evalId }} </span>
                              <button
                                [ngClass]="evalResult.finalEvalStatus == 1 ? 'result-btn pass': 'result-btn fail'" (click)="getHistorySession(evalResult)">
                                <span class="material-symbols-outlined">
                                  {{ evalResult.finalEvalStatus == 1 ? "check": "close" }}
                                </span>
                                <div style="padding-top: 4px;">{{ evalResult.finalEvalStatus == 1 ? i18n.passStatusCaps : i18n.failStatusCaps}} </div>
                              </button>
                            </div>
                          </div>
                        }
                      </div>
                    }
                  </div>
                </div>
              }
            </div>
          }
        </div>
      }
      @if (!evalRunning() && !showEvalHistory()) {
        <button class="save-session-btn" (click)="openNewEvalCaseDialog()">
          <div class="save-session-btn-detail">
            <mat-icon>add</mat-icon>
            <div class="save-session-btn-text">
              {{ i18n.addSessionToSetButtonPrefix }} {{selectedEvalSet}}
            </div>
          </div>
        </button>
      }
      @if (evalRunning()) {
        <div>
          <mat-spinner class="eval-spinner" [diameter]="28" [strokeWidth]="3"></mat-spinner>
        </div>
      }
    </div>
  }
</div>

